<?php

namespace Form\Store;

use App\Dao\Exam\ExamDao;
use App\Dao\Task\TaskHelpDao;
use App\Dao\Task\TaskTypesDao;
use Form\BaseForm;
use App\Model\Task\TaskHelpCategory;
use Yj\Apidog\Annotation\ApiController;
use Yj\Apidog\Annotation\FormData;
use Yj\Apidog\Annotation\PostApi;
use Yj\ColumnBuilder\Column;
use Yj\ColumnBuilder\Table;
use Yj\ColumnBuilder\Templates\Field;
use Yj\ColumnBuilder\Templates\FieldTemplate;
use Yj\ColumnBuilder\Templates\InputTemplate;
use Yj\Form\Elm;

/**
 * @ApiController(prefix="form/store/service/feedback")
 */
class ChatFeedback extends \Yj\Form\BaseForm
{
    protected $yjCode = 'store/service/feedback';

    public function getColumn()
    {
        $colum = Column::create();
        $colum->setRules([
            Column::txt()->setLabel('uid')->setProp('uid'),
            Column::image()->setLabel('头像')->setProp('avatar'),
            Column::txt()->setLabel('用户昵称')->setProp('nickname'),
            Column::txt()->setLabel('手机号码')->setProp('phone'),
            Column::txt()->setLabel('会员等级')->setProp('level'),
            Column::txt()->setLabel('余额')->setProp('now_money'),
            Column::txt()->setLabel('小豆')->setProp('integral'),
            Column::txt()->setLabel('用户类型')->setProp('user_type'),
            Column::switch()->setLabel('状态')->setProp('status'),
        ]);

        return $colum->toArray();
    }

    public function getTabs()
    {
        return Table::header()->setProp('user_type', 'all')->setOptions([
            ['label' => 'App', 'value' => 'app'],
            ['label' => '微信公众号', 'value' => 'wechat'],
            ['label' => '微信小程序', 'value' => 'routine'],
            ['label' => 'H5', 'value' => 'h5'],
            ['label' => 'PC', 'value' => 'pc'],
        ])->toArray();
    }

    public function getSearch()
    {
        return Table::search()->setFields([
            Field::input()->setLabel('用户手机号')->setVModel('phone'),
            Field::input()->setLabel('用户昵称')->setVModel('nickname'),
        ])->toArray();
    }

    /**
     * @PostApi(path="config",description="配置")
     */
    public function config()
    {
        $routes = $this->getRoute();
        $column = $this->getColumn();
        $search = $this->getSearch();
        $tabs = $this->getTabs();
        _SUCCESS(compact('routes', 'column', 'tabs', 'search'));
    }

    /**
     * @PostApi(path="update",description="添加")
     * @FormData(key="id",rule="nullable")
     */
    public function update()
    {
        $id = $this->getParams()->get('id');
        $form = Elm::createForm($this->getRoute($id ? 'edit' : 'add'));
        $form->setRule([
            Elm::YjUpload()->field('image_id')->title('图标'),
            Elm::input('title', '标题')->required(),
            Elm::textarea('desc', '内容')->required(),
        ]);
        $id && $form->append(
            Elm::hidden('id', $id)
        );
        $lists = $form->setTitle($id ? '编辑' : '添加' . '任务帮助')->formData(function () use ($id) {
            $formData = [];
            if ($id) {
                $formData = App(TaskTypesDao::class)->where('id', $id)->first();
            }
            return $formData;
        });
        _SUCCESS(formToData($lists));
    }
}